package thread;


public class SyncDemo1 {
    private static boolean success1=false;
    private static boolean success2=false;
    public static void main(String[] args)throws InterruptedException {
        Bank bank=new Bank();
        Thread t1=new Thread(){
            public void run(){
                success1=bank.getMoney(20000);
            }
        };
        Thread t2=new Thread(){
            public void run(){
                success2=bank.getMoney(20000);
            }
        };
        t1.start();
        t2.start();
        Thread.sleep(20);
        if(success1&&success2){
            System.out.println("两人都取款成功!卡BUG成功!");
        }else{
            System.out.println("仅有一人取款成功");
        }
    }
}
class Bank{
    private int account=20000;
    //synchronized同步
    public synchronized boolean getMoney(int money){
        int account=getAccount();

        if(account>=money){
            account=account-money;
            Thread.yield();//该方法让线程主动放弃本次时间
            saveAccount( account);
            return true;
        }
            return false;
        }
        public int getAccount(){
            return account;
        }
        public void saveAccount(int account){
            this.account=account;
        }
}